<template>
  <base-breadcrumb v-loading="loading.isLoad">
    <a-card class="mb20">
      <titleName :title="form.meetingName" />
      <a-form-model
        ref="form"
        :model="form"
        :rules="rules"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
      >
        <a-row :gutter="24">
          <a-col
            :md="16"
            :sm="16"
          >
            <a-form-model-item label="所属单位：">
              <BaseInput
                v-model="form.viewName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 7"
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="交底方案："
              prop="confessName"
            >
            <BaseInput
                v-model="form.confessName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if=" form.meetingTypeCategory == 4"
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="月份：">
              <a-month-picker
                v-model="form.month"
                placeholder="加载中"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if=" form.meetingTypeCategory == 7"
          >
            <a-form-model-item label="会议名称：">
              <BaseInput
                v-model="form.meetingName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if="form.meetingTypeCategory == 7"
          >
            <a-form-model-item label="是否为专业分包方案：" >
              <BaseInput
                v-model="form.isProSubName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if=" form.meetingTypeCategory == 7"
          >
            <a-form-model-item label="方案类型：" >
              <BaseInput
                v-model="form.planType"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if=" form.meetingTypeCategory == 7"
          >
            <a-form-model-item label="现行版本：" >
              <BaseInput
                v-model="form.currentVersion"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if=" form.meetingTypeCategory == 7"
          >
            <a-form-model-item label="编制人员：" >
              <BaseInput
                v-model="form.programmer"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="主持人/主讲人：">
              <BaseInput
                v-model="form.speaker"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="会议地点：">
              <BaseInput
                v-model="form.meetingPlace"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="参会人数：">
              <span class="flex">
                <BaseInput
                  v-model="form.meetingCount"
                  style="width: 100%"
                  disabled
                  placeholder="加载中"
                />
                <a-button
                  v-if="
                    form.meetingTypeCategory == 1 ||
                      form.meetingTypeCategory == 2 ||
                      form.meetingTypeCategory == 3 ||
                      form.meetingTypeCategory == 4 ||
                      form.meetingTypeCategory == 5 ||
                      form.meetingTypeCategory == 6 ||
                      (form.meetingTypeCategory == 7 && !isProfessional)
                  "
                  class="ml20"
                  type="primary"
                  @click="signInVsible = true"
                >查看签到</a-button>
                <a-button
                  v-if="
                    form.meetingTypeCategory != 8 &&
                      form.meetingTypeCategory != 9 &&
                      form.status != 35 &&
                      form.status != 40
                  "
                  class="ml20"
                  type="primary"
                  @click="handlePage"
                >会议大屏</a-button>
              </span>
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
            v-if="form.meetingTypeCategory != 4"
          >
            <a-form-model-item label="交底人：" >
              <BaseInput
                v-model="form.confessUserName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="召开时间：">
              <BaseInput
                v-model="form.startTime"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
            <a-form-model-item label="记录人：" v-if="!form.isProSub" >
              <BaseInput
                v-model="form.creatorName"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            :md="8"
            :sm="8"
          >
           <!-- form.isProSub 是专业分包不显示 -->
            <a-form-model-item label="记录时间：" v-if="!form.isProSub">
              <BaseInput
                v-model="form.createTime"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 7 && !form.isProSub"
            :md="8"
            :sm="8"
          >
            <!-- <a-form-model-item
              label="交底方案："
              prop="commentedPlan"
            >
              <BaseInput
                v-model="form.commentedPlan"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item> -->
            <a-form-model-item label="实际签到人数：">
              <BaseInputNumber
                    v-model="form.meetingSignInNum"
                    disabled
                    placeholder="加载中"
                />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 7 || form.meetingTypeCategory == 8"
            key="meetingExplains"
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="会议说明："
              prop="meetingExplains"
            >
              <BaseTextarea
                v-model.trim="form.meetingExplains"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 7 && isProfessional"
            key="meetingExplains2"
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="主要参会人员："
              prop="mainParticipants"
            >
              <BaseTextarea
                v-model.trim="form.mainParticipants"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 7 && isProfessional"
            key="meetingSummary"
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="会议纪要："
              prop="meetingSummary"
            >
              <BaseTextarea
                v-model.trim="form.meetingSummary"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col 
            :md="24"
            :sm="24" v-if="form.meetingTypeCategory == 7 && isProfessional">
            <a-form-model-item label="现场照片">
                <upload-list
                    :businessCode="'SHE04'"
                    :uploadArrys.sync="form.sitPhotoList"
                    @fileArrysData="handleFileUploaded1"
                    uploadType="image"
                    :editModel="0"
                    multiple
                ></upload-list>
            </a-form-model-item>
        </a-col>
        <a-col  :md="24"
            :sm="24"
         v-if="form.meetingTypeCategory == 7 && isProfessional">
            <a-form-model-item label="附件上传">
                <upload-list
                    :businessCode="'SHE04'"
                    :uploadArrys.sync="form.summaryFileList"
                    @fileArrysData="handleFileUploaded2"
                    :editModel="0"
                    disabled
                    multiple
                ></upload-list>
            </a-form-model-item>
        </a-col>
          <a-col
            v-if="
              form.meetingTypeCategory == 8 ||
                form.meetingTypeCategory == 9
            "
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="主要参会人员："
              prop="mainParticipants"
            >
              <BaseTextarea
                v-model.trim="form.mainParticipants"
                disabled
                placeholder="加载中      "
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 5"
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="教育课时："
              prop="educationHour"
            >
              <BaseInput
                v-model="form.educationHour"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 5"
            :md="8"
            :sm="8"
          >
            <a-form-model-item
              label="教育负责人："
              prop="educationResponsible"
            >
              <BaseInput
                v-model="form.educationResponsible"
                disabled
                placeholder="加载中"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory == 5"
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="受教育单位："
              prop="educationOrganization"
            >
              <BaseTextarea
                v-model="form.educationOrganization"
                disabled
                placeholder="请输入"
              />
            </a-form-model-item>
          </a-col>
          <a-col
            v-if="form.meetingTypeCategory !== 7"
            :md="24"
            :sm="24"
          >
            <a-form-model-item
              label="会议纪要："
              prop="meetingSummary"
            >
              <!-- <editor-bar
                                v-if="editor"
                                :disabled="pageType != 'edit'"
                                @change="commentsVal"
                                :maxLength="30000"
                                v-model="form.meetingSummary"
                                :isClear="isClear"
                                style="width: 100%"
                            ></editor-bar> -->
              <BaseTextarea
                v-if="pageType !== 'look'"
                v-model="form.meetingSummary"
                :auto-size="{ minRows: 20, maxRows: 25 }"
                :max-length="30000"
                :disabled="pageType != 'edit'"
                placeholder="请输入"
              />
              <div
                v-else
                style="max-height: 300px; overflow: auto; background-color: #f5f5f5"
                v-html="form.meetingSummary"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <div v-if="form.meetingTypeCategory == 7">
      <a-card
        v-if="pageType !== 'start' && !isProfessional"
        class="mb20"
      >
        <titleName title="会议纪要" />
        <a-form-model
          ref="form7"
          :model="form"
          :rules="rules"
          :label-col="{ span: 6 }"
          :wrapper-col="{ span: 16 }"
        >
          <a-row :gutter="24">
            <a-col 
            :md="24"
            :sm="24" >
            <a-form-model-item label="现场照片">
              <upload-list
                    :businessCode="'SHE04'"
                    :uploadArrys.sync="form.sitPhotoList"
                    uploadType="image"
                    :editModel="2"
                  :disabled="pageType != 'edit'"
                  multiple
                ></upload-list>
            </a-form-model-item>
        </a-col>
            <a-col
              :md="24"
              :sm="24"
            >
              <a-form-model-item
                label="主要内容："
                prop="meetingSummary"
              >
                <BaseTextarea
                  v-if="pageType !== 'look'"
                  v-model="form.meetingSummary"
                  :auto-size="{ minRows: 20, maxRows: 25 }"
                  :max-length="30000"
                  :disabled="pageType != 'edit'"
                  placeholder="请输入"
                />
                <div
                  v-else
                  style="max-height: 300px; overflow: auto; background-color: #f5f5f5;word-break: break-all;word-wrap: break-word;"
                  v-html="form.meetingSummary"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col
              :md="24"
              :sm="24"
            >
              <a-form-model-item label="附件上传：">
                <upload-list
                  :business-code="'SHE04'"
                  :upload-arrys.sync="form.fileList"
                  :disabled="pageType != 'edit'"
                  multiple
                  @fileArrysData="handleFileUploaded"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
    </div>

    <a-card
      v-else
      class="mb20"
    >
      <titleName title="相关资料上传" />
      <a-form-model ref="ruleForm">
        <div
          v-if="pageType != 'look' && pageType != 'start'"
          class="table-operator row"
        >
          <l-table
            upload-type="normal"
            text="新增"
            btn-type="primary"
            :upload-arrys="form.fileList"
            business-code="SHE04"
            @fileArrysData="fileArrysData"
          />

          <!-- <a-button class="ml20">删除</a-button> -->
        </div>
        <s-table
          ref="table"
          bordered
          size="default"
          row-key="key"
          :columns="columns"
          :data="loadData"
          :alert="true"
          show-size-changer
          show-quick-jumper
          :show-pagination="false"
        >
          <span
            slot="serial"
            slot-scope="text, record, index"
          >{{ index + 1 }}</span>
          <span
            slot="action"
            slot-scope="text, record, index"
          >
            <a
              :href="
                '/api/ycloud-file-center/oper/download?access_token=' +
                  token +
                  '&fileId=' +
                  record.fileId
              "
            >下载</a>
            <a-divider type="vertical" />
            <a
              href="javascript:"
              @click="$filePreview(record)"
            >查看</a>
            <a-divider
              v-if="pageType != 'look' && pageType != 'start'"
              type="vertical"
            />
            <a
              v-if="pageType != 'look' && pageType != 'start'"
              @click="delItem(index)"
            >删除</a>
          </span>
        </s-table>
      </a-form-model>
    </a-card>
    <seeSign
      :id="id"
      :visible="signInVsible"
      @cancel="signInVsible = false"
      @confirm="signInVsible = false"
    />
    <DisclosureRecord v-if="form.projectId && pageType == 'update'" :formData="form"></DisclosureRecord>
    <DisclosureRecordDetail v-if="form.projectId && pageType == 'look'"  :formData="form"></DisclosureRecordDetail>
    <div v-if="pageType !== 'update'">
      <div
      v-if="pageType !== 'look'"
      class="tool-btns page-btn-right-top"
    >
      <!-- <a-button
                v-if="id != '' && formInfo.auditStatus == 'DRAFT'"
                type="danger"
                :loading="loading.DELETE"
                @click="handleSave('DELETE')"
                >删除草稿</a-button
            > -->
      <!-- <a-button
                v-if="pageType != 'start'"
                type="primary"
                :disabled="form.meetingSummary == ''"
                :loading="loading.DRAFT"
                @click="handleSave('DRAFT')"
                >保存草稿</a-button
            > -->
      <a-button
        v-if="pageType == 'start' && currentUer"
        type="primary"
        :loading="loading.START"
        @click="handleSave('START')"
      >
        开始会议
      </a-button>
      <a-button
        v-if="pageType != 'start' && currentUer"
        type="primary"
        :loading="loading.SUBMITTED"
        @click="handleSave('SUBMITTED')"
      >
        提交
      </a-button>
      <a-button
        type="primary"
        @click="handleClose"
      >
        关闭
      </a-button>
    </div>
    <div
      v-else
      class="tool-btns page-btn-right-top"
    >
      <BasePrintButton
        v-if="
          form.meetingTypeCategory == '5' ||
            form.meetingTypeCategory == '6' ||
            form.meetingTypeCategory == '7' && !form.isProSub
        "
        type="primary"
        :before-print-fn="getMeetingPrintReq"
      />
      <a-button
        type="primary"
        class="ml20"
        @click="handleClose"
      >
        关闭
      </a-button>
    </div>
    </div>
  </base-breadcrumb>
</template>
<script>
import { STable } from '@/components'
import UploadList from '@/components/UploadList'
import DisclosureRecord from './disclosureRecord'   //新增交底记录
import DisclosureRecordDetail from './disclosureRecordDetail'   //查看交底记录

import { getMeeting, meetingSummary, startMeeting, printMeetingDetail } from '@/api/office/conference'
import seeSign from '@/components/seeSign'
import { getMeetingPrint } from '@/api/safetySupervision/metting'
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
const columns = [
    {
        title: '序号',
        dataIndex: 'serial',
        width: '5%',
        scopedSlots: { customRender: 'serial' }
    },
    {
        title: '文件名称',
        dataIndex: 'fileName',
        width: '40%'
    },
    {
        title: '文件格式',
        dataIndex: 'suffixName'
    },
    {
        title: '操作',
        dataIndex: 'action',
        width: '15%',
        scopedSlots: { customRender: 'action' }
    }
]

import Vue from 'vue'
export default {
    components: {
        STable,
        seeSign,
        BasePrintButton,
        UploadList,
        DisclosureRecord,
        DisclosureRecordDetail
    },
    data() {
        return {
            signInVsible: false,
            loading: {
                DELETE: false,
                DRAFT: false,
                SUBMITTED: false,
                START: false,
                isLoad: false,
            },
            columns,
            isClear: false,
            form: { fileList: [], meetingSummary: '', viewName: '',sitPhotoList:[]},
            visible: false,
            rules: {
                meetingSummary: [{ required: true, message: '请填写会议纪要', trigger: 'change' }]
            },
            id: '',
            pageType: '',
            token: Vue.getAcloudProvider().getAccessToken(),
            loadData: () => {
                return new Promise((resolve, reject) => {
                    resolve({
                        data: this.form.fileList
                    })
                })
            },
            editor: true,
            isProfessional:false, //是否专业分包，true是

        }
    },
    watch: {
        '$route': {
            handler(to, from) {
                if(this.id !== this.$route.query.id) {
                    this.initPage()
                }
            }
        }
    },
    computed: {
        currentUer() {
            const userInfo = Vue.getAcloudProvider().getUserInfo()
            console.log(userInfo.name == this.form.creatorName)
            return userInfo.name == this.form.creatorName
        }
    },
    mounted() {
        this.initPage()
    },
    methods: {
        initPage() {
        if (this.$route.query.id) {
            this.id = this.$route.query.id
            this.pageType = this.$route.query.pageType
            this.initTitle()
            this.getMeeting()
        }
        },
        initTitle() {
            if (this.pageType == 'edit') {
                this.$route.meta.title = '填写会议纪要'
            } else {
                this.$route.meta.title = '会议详情'
            }
        },
        commentsVal: function (val) {
            console.log(val)
            this.form.meetingSummary = val
        },
        validateForm() {
            let flag = false
            this.$refs.form.validate((valid) => {
              if(!valid) {
                this.$message.warning("请填写完所有必填项信息")
              }
                flag = valid
            })
            if (this.form.meetingTypeCategory == 7) {
              this.$refs.form7.validate((valid) => {
                  flag = valid
              })
            }
            return flag
        },
        // 处理增、删、改操作
        async handleSave(status) {
          if(status=='SUBMITTED'){
                if(this.form.meetingTypeCategory*1 === 7){
                  if (!this.form.sitPhotoList || this.form.sitPhotoList?.length < 1 ){
                this.$message.warning('请上传现场照片');
                return 
                }
                if (!this.form.fileList || this.form.fileList?.length < 1 ){
                  this.$message.warning('请上传附件上传');
                  return 
                }
              }
            }
            if (this.pageType == 'start') {
                this.loading[status] = true
                const [err, res] = await startMeeting({ id: this.id })
                this.loading[status] = false
                this.editor = false
                this.$nextTick(() => {
                    this.pageType = 'edit'
                    this.editor = true
                })

                this.getMeeting()
                this.initTitle()
                return
            }
            if (!this.validateForm()) return
            if (this.loading[status]) return
            this.loading[status] = true
            const params = {
                ...this.form,
                status: status == 'DRAFT' ? 10 : 20
            }
            // const { pageType } = this.$route.query
            // const fn = pageType === 'edit' ? meetingSummary : meetingSummary
            // const map = {
            //     DRAFT: meetingSummary,
            //     SUBMITTED: meetingSummary,
            // }
            if(this.form.meetingTypeCategory*1===7){
              let that = this;
                this.$confirm({
                title: '提示',
                content: `本次需参会人数${this.form.meetingCount}，已签到人数${this.form.meetingSignInNum}，请慎重提交`,
                okText: '确定',
                cancelText: '取消',
                type: 'warning',
                onOk: async () => {
                  const [err, res] = await meetingSummary(params)
                      if (!err) {
                        that.$message.success('成功')
                        that.handleClose()
                      }
                      that.loading[status] = false
                },
                onCancel() {
                  that.loading[status] = false
                },
              })
            }else{
              const [err, res] = await meetingSummary(params)
            if (!err) {
                this.$message.success('成功')
                this.handleClose()
            }
            this.loading[status] = false
            }
     
        },
        getMeetingPrintReq() {
            if (this.form.meetingTypeCategory == '7') {
                return printMeetingDetail({ id: this.id }).then((res) => {
                    return res[1].data.content
                })
            } else {
                return getMeetingPrint({
                    id: this.id,
                    type: this.$route.query.printType === 'education' ? 'AQJYJJ' : 'AQGJ'
                }).then((res) => {
                    return res[1].data.content
                })
            }
        },
        async getMeeting() {
            this.loading.isLoad = true
            const [err, res] = await getMeeting({ id: this.id })
            if (!err) {
                this.form = res.data
                if (this.form.meetingTypeCategory == '7') {
                  this.form.isProSubName =  this.form.isProSub?'是':'否';
                  this.isProfessional = this.form.isProSub;
                }
                this.form.viewName = this.form.branchComName + '/' + this.form.projectName
                this.$refs.table && this.$refs.table.refresh && this.$refs.table.refresh(true)
            }
            this.loading.isLoad = false
        },
        delItem(idx) {
            this.form.fileList.splice(idx, 1)
            this.$refs.table.refresh(true)
            console.log(this.form.fileList)
        },
        fileArrysData: function (val) {
            this.form.fileList = val
            this.$refs.table.refresh(true)
        },
        handleFileUploaded(list) {
            this.form.fileList = [...list]
        },
        handleFileUploaded1(list) {
            this.form.fileList = [...list]
        },
        handleFileUploaded2(){

        },
        onSearch() {
            this.visible = true
        },
        handleClose() {
            this.$multiTab.close(this.$route.fullpath)
        },
        onSelectChange(e) {
            console.log('selectedRowKeys changed: ', e)
        },
        handlePage() {
            let routeData = this.$router.resolve({
                path: '/conference/standardConference/conferenceList/conferenceScreen',
                query: {
                    id: this.id
                }
            })
            window.open(routeData.href, '_blank')
        }
    }
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';
.tool-btns {
    display: flex;
    justify-content: flex-end;
    @{deep} .ant-btn + .ant-btn {
        margin-left: 12px;
    }
}
.flex {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    height: 40px;
}
</style>
